package com.tengfei.main.index.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tengfei.common.constant.Constants;
import com.tengfei.common.utils.redis.RedisUtils;
import com.tengfei.main.course.domain.Course;
import com.tengfei.main.course.mapper.CourseMapper;
import com.tengfei.main.index.mapper.IndexMapper;
import com.tengfei.main.index.service.IIndexService;
import com.tengfei.main.question.mapper.ObjectiveMapper;
import com.tengfei.main.question.mapper.SubjectiveMapper;
import com.tengfei.main.system.domain.SysUser;
import com.tengfei.main.system.mapper.SysUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author feige
 * @version v1.0
 * @date 2022-08-16-10:35
 * @description
 */

@Service
public class IndexServiceImpl implements IIndexService {

    @Resource
    private SysUserMapper userMapper;
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private ObjectiveMapper objectiveMapper;
    @Resource
    private SubjectiveMapper subjectiveMapper;
    @Resource
    private IndexMapper indexMapper;


    /**
     * tab选项卡数据
     *
     * @return
     */
    @Override
    public Map<String, Integer> tabData() {
        Map<String, Integer> map = new HashMap<>();
        // 总人数
        Long userNum = userMapper.selectCount(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getShowStatus, 1));
        map.put("userNum", userNum.intValue());
        // 在线人数
        // 获取所有未过期的 token
        List<String> keys = StpUtil.searchTokenValue("", -1, 0);
        int count = 0;
        for (String key : keys) {
            String token = key.replace(Constants.LOGIN_TOKEN_KEY, "");
            // 如果已经过期则踢下线
            if (StpUtil.stpLogic.getTokenActivityTimeoutByToken(token) < 0) {
                continue;
            }
            // 如果redis非空
            if (ObjectUtil.isNotNull(RedisUtils.getCacheObject(Constants.ONLINE_TOKEN_KEY + token))) {
                count++;
            }
        }
        map.put("onlineNum", count);
        // 总课程数
        Long courseNum = courseMapper.selectCount(new LambdaQueryWrapper<Course>()
                .eq(Course::getShowStatus, 1));
        map.put("courseNum", courseNum.intValue());
        // 总题目数
        Integer objNum = objectiveMapper.selectCount(null).intValue();
        Integer subNum = subjectiveMapper.selectCount(null).intValue();
        map.put("questionNum", objNum + subNum);
        return map;
    }

    /**
     * 轮播图数据
     *
     * @return
     */
    @Override
    public Map<String, Object> chartsData() {
        // 过去十天用户人数变化
        List<Map> ofTenUsers = indexMapper.selectOfTenUsers();
        // 过去十天课程数变化
        List<Map> ofTenCourses = indexMapper.selectOfTenCourses();
        // 过去十天题目数量变化
        // 客观题
        List<Map> ofTenObjQus = indexMapper.selectOfTenObjQus();
        // 主观题
        List<Map> ofTenSubQus = indexMapper.selectOfTenSubQus();
        Map<String, Object> datas = new HashMap<>();
        datas.put("users", ofTenUsers);
        datas.put("courses", ofTenCourses);
        datas.put("objs", ofTenObjQus);
        datas.put("subs", ofTenSubQus);
        return datas;
    }


}
